package cn.edu.hit.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.hit.db.DBTools;
import cn.edu.hit.javabean.*;

public class UserService {
	private static DBTools dbtools = new DBTools();
	public static boolean login(String username,String password, String identify){
		//System.out.println(username+","+password+","+identify);
		try {
			if (identify.equals("student")) {
				String sql = "select * from student where student_number=? and password=?";
				PreparedStatement ps = DBTools.getConnection().prepareStatement(sql);
				ps.setString(1, username);
				ps.setString(2, password);
				ResultSet rs = ps.executeQuery();

				if (rs != null && rs.next()) {
					return true;
				}
			}else if(identify.equals("admin")){
				String sql = "select * from admin where id=? and password=?";
				PreparedStatement ps = DBTools.getConnection().prepareStatement(sql);
				ps.setString(1, username);
				ps.setString(2, password);
				ResultSet rs = ps.executeQuery();

				if (rs != null && rs.next()) {
					return true;
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;	
	}
	
	/**
	 * 注册学生
	 * @param student 学生对象
	 * @return 成功返回true，否则返回false
	 */
	public static boolean register(Student student){
		try{
			if(student == null||student.getSex()==null)
				return false;
			String password = student.getPassword();
			String sql = "update student set password=?,sex=?,college=?,major=?,political_aff=?,nation=?,phone=?, isRegisted=1 where student_number=? and student_name=?";
			if(student.getOldPassword()!=null){
				sql += " and password=?";
				if(password.equals(""))
					password=student.getOldPassword();
			}
			PreparedStatement ps = DBTools.getConnection().prepareStatement(sql);
			ps.setString(1, password);
			ps.setString(2, student.getSex());
			ps.setString(3, student.getCollege());
			ps.setString(4, student.getMajor());
			ps.setString(5, student.getPolitic());
			ps.setString(6, student.getNation());
			ps.setString(7, student.getPhone());
			ps.setString(8, student.getStudentNumber());
			ps.setString(9, student.getStudentName());
			
			if(student.getOldPassword()!=null){
				ps.setString(10, student.getOldPassword());
			}
			System.out.println(ps.toString());
			return (ps.executeUpdate()>0);
		}catch(Exception exception){
			exception.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 修改管理员密码
	 * @param username 用户名
	 * @param oldpassword 原密码
	 * @param newpassword 新密码
	 * @return 是否成功，成功返回true，否则返回false
	 */
	public static boolean changePass(String username, String oldpassword, String newpassword){
		try {
			String sql = "update admin set password=? where id=? and password=?";
			PreparedStatement ps = DBTools.getConnection().prepareStatement(sql);
			ps.setString(1, newpassword);
			ps.setString(2, username);
			ps.setString(3, oldpassword);
			//System.out.println(ps.toString());
			boolean result = ps.executeUpdate()>0;
			ps.close();
			return result;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean addUser(User user){
		try{
			String sql = "insert into admin values(?,?,?,?)";
			PreparedStatement ps = DBTools.getConnection().prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPassword());
			ps.setString(3, user.getRole());
			ps.setString(4, user.getRealname());
			boolean result = ps.executeUpdate()>0;
			ps.close();
			return result;
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
}
